package com.nebula.api.modules.app.service.impl;

import com.nebula.api.modules.app.service.AppUserService;
import com.nebula.commons.modules.form.LoginForm;
import com.nebula.commons.modules.form.PasswordForm;
import com.nebula.commons.modules.system.entity.SysUser;
import com.nebula.commons.modules.system.service.SysUserService;
import link.ahsj.core.utils.base.AppAssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class AppUserServiceImpl implements AppUserService {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 用户修改密码
     *
     * @param sysUser 用户实体
     * @param form    用户修改密码实体
     * @return 状态值
     */
    @Override
    public boolean updatePsw(SysUser sysUser, PasswordForm form) {
        // 通过登录用户获取当前密码
        String password = sysUser.getPassword();
        //验证旧密码正确方可修改为新密码
        AppAssertUtil.isErr(!password.equals(form.getOldPassword()), "您输入的密码不匹配.");
        //新密码不应该与旧密码相同
        AppAssertUtil.isErr(password.equals(form.getNewPassword()), "请不要使用原密码.");
        //用户的旧密码不应为新密码
        AppAssertUtil.isErr(form.getNewPassword().equals(sysUser.getOldPassword()), "请不要使用原密码.");
        //修改密码逻辑:密码更新
        sysUser.setPassword(form.getNewPassword());
        //旧密码记录ss
        sysUser.setOldPassword(password);

        return sysUserService.updateById(sysUser);
    }


    /**
     * 用户登陆
     *
     * @param loginForm
     * @return
     */
    @Override
    public SysUser login(LoginForm loginForm) {
        SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getUsername, loginForm.getUsername()).one();
        AppAssertUtil.isNull(sysUser, "用户名或密码错误.");
        return sysUser;
    }

}
